<?php
function HasLogin()
{
	return 	isset($_COOKIE["gwt_userid"]) && $_COOKIE["gwt_userid"] != "" &&
			isset($_COOKIE["gwt_nickname"]) && $_COOKIE["gwt_nickname"] != "" &&
			isset($_COOKIE["gwt_zfb"]) && $_COOKIE["gwt_zfb"] != "";
}
function MakeRecommendCodeFrom($id, $type='N')
{
	if ($type=='')
		$type='N';
	$id += 123987;
	return strtoupper($type.dechex($id));
}
function GetRecommendIdFromCode($inCode, &$type='')
{
	if ($inCode=="")
		return "";

	$inCode = strtoupper($inCode);
	$type = substr($inCode, 0, 1);
	if ($type == 'U' || $type == 'V' || $type =='Y' || $type == 'N')
	{
		$id = substr($inCode, 1, strlen($inCode) - 1);
		$id = hexdec($id);
		$id -= 123987;
		return $id;
	}
	else	// old
	{
		$type = 'N';
		return $inCode;
	}
}
function GetTubi($level)
{
	$LEVEL1 = 300;
	$LEVEL2 = 2400;
	$LEVEL3 = 4800;
	$LEVEL4 = 12000;
	$LEVEL5 = 40000;
	$LEVEL6 = 300000;
	$LEVEL7 = 1000000;
	
	$retLevelMax = 0;
	$retLevelDesc = "";
	$retLevel = 0;
	
	if($level<$LEVEL1){
		$retLevelMax = $LEVEL1;
		$retLevelDesc = "牙牙兔";
		$retLevel = 1;
	}
	else if($level>=$LEVEL1 && $level<$LEVEL2){
		$retLevelMax = $LEVEL2;
		$retLevelDesc = "小兔丁";
		$retLevel = 2;
	}
	else if($level>=$LEVEL2 && $level<$LEVEL3){
		$retLevelMax = $LEVEL3;
		$retLevelDesc = "初出兔窟";
		$retLevel = 3;
	}
	else if($level>=$LEVEL3 && $level<$LEVEL4){
		$retLevelMax = $LEVEL4;
		$retLevelDesc = "江湖兔侠";
		$retLevel = 4;
	}
	else if($level>=$LEVEL4 && $level<$LEVEL5){
		$retLevelMax = $LEVEL5;
		$retLevelDesc = "一代宗兔";
		$retLevel = 5;
	}
	else if($level>=$LEVEL5 && $level<$LEVEL6){
		$retLevelMax = $LEVEL6;
		$retLevelDesc = "天兔合一";
		$retLevel = 6;
	}
	else if($level>=$LEVEL6 && $level<$LEVEL7){
		$retLevelMax = $LEVEL7;
		$retLevelDesc = "孤兔求败";
		$retLevel = 7;
	}

	return array("level"=>$retLevel, "level_max"=>$retLevelMax, "level_desc"=>$retLevelDesc);
}


function ReturnFalse( $err )
{
	echo "hr=1\n";
	echo "err=\"$err\"\n";
}

function ReturnNoLogin( $err )
{
	echo "hr=-1\n";
	echo "err=\"$err\"\n";
}

function ReturnTrue()
{
	echo "hr=0\n";
}

function WriteReturnLine( $key, $value )
{	
	echo "$key=$value\n";
}

function UpdateSoftList( $tableName, $RevId )
{
	$sql = "select Exe from $tableName where RevId>$RevId";
	$result = mysql_query( $sql ) or die( "sql error: $sql" );
	$row_num = mysql_num_rows( $result );
	
	if( $row_num > 0 )
	{
		$title = $tableName . "exe";
		$exe = "";
		while( $row = mysql_fetch_array( $result ) )
		{
			if( $row["bException"] == 1 )
				$exe .= "-". $row["Exe"] . ",";
			else
				$exe .= $row["Exe"] . ",";
		}
		
		if( $exe != "" )
		{
			$exe = DeleteLastChar( $exe, "," );
			WriteReturnLine( $title, $exe );
		}
	}
}

function DeleteLastChar( $str, $char )
{
	if( substr( $str, - 1 ) == $char )
	{
		$str = substr( $str, 0 ,strlen( $str ) - 1  );
	}
	
	return $str;
}

function CheckUrlParam( $param )
{
	if( isset( $_GET[$param] ) )
	{
		return $_GET[$param];
	}
	else
	{
		ReturnFalse( "no param: $param" );
		die( "" );
	}
}

function CheckSessionParam( $param )
{
	if( isset( $_SESSION[$param] ) )
	{
		return $_SESSION[$param];
	}
	else
	{
		ReturnNoLogin( "no param: $param" );
		die( "" );
	}
}

function CheckAdminPassword( )
{
	if( isset( $_SESSION["admin_password"] ) )
	{
		if( $_SESSION["admin_password"] != "admin_ocheng")
			die("No Admin Login.");
	}
	else
	{
		die( "No Admin Login." );
	}
}

function CheckUrlParamNoDie( $param )
{
	if( isset( $_GET[$param] ))
		return $_GET[$param];
	else
		return "";
}

function SetUrlParamInt( $param )
{
	if( isset( $_GET[$param] ))
		return intval($_GET[$param]);
	else
		return 0;
}

function GetPostParamInt( $param )
{
	if( isset( $_POST[$param] ))
		return intval($_POST[$param]);
	else
		return 0;
}

function GetPostParamFloat( $param )
{
	if( isset( $_POST[$param] ))
		return floatval($_POST[$param]);
	else
		return 0;
}

function GetPostParamStr( $param )
{
	if( isset( $_POST[$param] ))
		return $_POST[$param];
	else
		return "";
}

function SetUrlParamIntNoDie( $param )
{
	if( isset( $_GET[$param] ))
		return intval($_GET[$param]);
	else
		return -1;
}

function UpdateInfo( $table, $updateid )
{
	$sql = "select * from $tableName where Id>$updateid";
	$result = mysql_query( $sql ) or die( "sql error : $sql" );
	$row_rum = mysql_num_rows( $result );
	$nIndex = 0;
	
	if( $row_num > 0 )
	{
		ReturnTrue();
		WriteReturnLine( "count", $row_num );
		while( $row = mysql_fetch_array( $result ) )
		{
			$nIndex++;
			WriteReturnLine( "id".$nIndex, $row['Id']);
			WriteReturnLine( "name".$nIndex, $row['Name']);
			WriteReturnLine( "content".$nIndex, $row['Content']);
			WriteReturnLine( "pubilctime".$nIndex, $row['PubilcTime']);
		}	
	}
	else
	{
		ReturnFalse("no need update.");
	}
}

function AddInfo( $table, $name , $content, $publictime )
{
	$sql = "insert into $table(name,content,publictime) values('$name','$content','publictime')";
	$result = mysql_query( $sql );
	if( $result )
		ReturnTrue();
	else
		ReturnFalse( "add item failed" );
}

function Login( $UserName, $Password )
{
	$sql = "select Id from parent where Name='$username'";

	$result = mysql_query( $sql );
	$row_num = mysql_num_rows( $result);
	
	if( $row_num > 0 )
	{
		$row = mysql_fetch_array( $result );
		if( $row["PWord"] == md5($password) )
		{
			$sql = "select Id from child where ParentId=$row[Id]";
			$result = mysql_query( $sql );
			$row_num = mysql_num_rows( $result );
			if( $row_num > 0 )
			{
				$row = mysql_fetch_array( $result );
				ReturnTrue();
				WriteReturnLine("childid", $row['Id']);
			}
			else
			{
				ReturnFalse("no child id.");
			}
		}
		else
		{
			// password error.
			return 3;
		}
	}
	else
	{
		//username not found
		return 1;
	}
}

function GetLastRevId()
{
	$resultId = array( );
	$resultId[] = GetLastId( "filmlist" );
	$resultId[] = GetLastId( "gamelist" );
	$resultId[] = GetLastId( "imlist" );
	$resultId[] = GetLastId( "browserlist" );
	$resultId[] = GetLastId( "unreportlist" );
	
	$RevId = $resultId[0];
	for( $i = count( $resultId ) - 1; $i >= 0; $i-- )
	{
		if( $resultId[$i] > $RevId )
			$RevId = $resultId[$i];
	}
	
	return $RevId;
}

function GetLastId( $tableName )
{
	$sql = "select MAX(RevId) from $tableName where 1";
	$result = mysql_query( $sql );
	$row = mysql_fetch_row( $result );
	return $row[0];
}

function DefaultBaseTable( $table, $childid )
{
	$sql = "insert into $table(ChildId) values($childid)";
	$result = mysql_query( $sql ) or die( "sql error:$sql" );
}

function InitUserTable( $childid )
{
	//init time table
	for( $i = 1; $i <= 6; $i++ )
	{
		$table = TypeToSetTimeTable( $i );
		DefaultBaseTable( $table, $childid );
	}
	
	//init child table
	$sql = "insert into child(ParentId) values($childid)";
	$result = mysql_query( $sql ) or die( "sql error:$sql" );
	
	//inti setting talbe 
	DefaultBaseTable( "setting", $childid );
	
	DefaultBaseTable( "heartbeat", $childid );
	
	DefaultBaseTable( "consoleheartbeat", $childid );
	
	DefaultBaseTable( "personalinfo", $childid );
	
	InitSetTimeUI( $childid );
}

function InitSetTimeUI( $childid )
{
	$sql = "insert into settimeui(ChildId,bFilm,bGame,bOnline,bUsePc,bChat,One,Two) values($childid,1,1,1,0,1,256,330)";
	mysql_query( $sql ) or die( $sql );
	
	$sql = "insert into settimeui(ChildId,bFilm,bGame,bOnline,bUsePc,bChat,One,Two) values($childid,0,0,1,0,1,38,231)";
	mysql_query( $sql ) or die( $sql );
}

function TypeToNow( $type )
{	
	switch( $type )
	{
		case 1:
			$day = 0;
		break;
		case 2:
			$day = intval(date("N")) - 1;
		break;
		case 3:
			$day = intval(date("d")) - 1;
		break;
		default:
		break;
	}
	$Day = GetNowUnixTimeStamp() - ( $day * 24 * 60 * 60 );
	$NowTime = date( "Y-m-d 00:00:00", $Day );
	return $NowTime;
}

function GetNowUnixTimeStamp()
{
	//return time() + 8 * 60 * 60;
	//目前服务器不用加8个小时
	return time();
}

function GetNoAddTime()
{
	return time();
}

function TypeToSetTimeTable( $type )
{
	$strResult = "filmlist";
	switch( $type )
	{
		case 1:
			$strResult = "gametime";
		break;
		case 2:
			$strResult = "onlinetime";
		break;
		case 3:
			$strResult = "movicetime";
		break;
		case 4:
			$strResult = "musictime";
		break;
		case 5:
			$strResult = "chattime";
		break;
		case 6:
			$strResult = "usepctime";
		break;
		default:
		break;
	}
	return $strResult;
}

function GetKey( $url, $key )
{
	$key = "&" . $key . "=";
	$nStart = stripos( $url, $key );
	if( $nStart !== false )
	{
		$nStart += strlen( $key );
		$nEnd = stripos( $url, "&", $nStart );

		if( $nEnd !== false )
		{
			return substr( $url, $nStart, $nEnd - $nStart );
		}
		else
		{
			return substr( $url, $nStart );
		}
	}
	return "";
}

function EnCode( $str, $key )
{
	$nStr = strlen( $str );
	$nKey = strlen( $key );
	$nCnt = $nStr / $nKey;
	
	$strCode = "";
	for( $i = 0; $i <= $nCnt; $i++ )
	{
		$strCode .= $key;
	}
	
	$strCode = substr( $strCode, 0, $nStr );
	
	return $str ^ $strCode;
}

?>
